package com.awo.blackfacedetect.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.awo.blackfacedetect.R;
import com.awo.facedetect.activity.FaceDetectActivity;
import com.awo.loglibrary.LogActivity;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks{

    private static final String TAG = "MainActivity";

     /*权限相关,将APP所需的权限在启动Activity中一次请求.
     相关逻辑已写完,需要什么权限在REQUIRED_PERMISSIONS中添加*/
    private static final int REQUEST_CODE = 111;
    private static final String[] REQUIRED_PERMISSIONS = new String[]{Manifest.permission.CAMERA,
    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.READ_PHONE_STATE, Manifest.permission.INTERNET};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 检查并请求所需权限
        checkAndRequestPermissions();
    }

    /**
     * 请求权限流程结束后调用，后续逻辑请写在这里，以保证先进行权限请求！
     */
    private void afterRequestPermissions() {
        startActivity(new Intent(MainActivity.this, SelectActivity.class)); // 测试人脸检测模组
    }

    /**
     * 请求结果的回调,固定写法
     * @param requestCode 权限请求代码
     * @param permissions 请求的权限
     * @param grantResults 授权结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    /**
     * 检查并请求未授权的权限
     */
    public void checkAndRequestPermissions() {
        if (!EasyPermissions.hasPermissions(this, REQUIRED_PERMISSIONS)) {
            EasyPermissions.requestPermissions(this, getString(R.string.requestPermissionsTip), REQUEST_CODE, REQUIRED_PERMISSIONS);
        }
    }

    /**
     * 权限均授权的回调
     * @param requestCode 权限请求代码
     * @param perms 请求的权限
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        Log.i(TAG, getString(R.string.allPermissionsGranted));
    }

    /**
     * 存在权限被拒绝授予时的回调
     * @param requestCode 权限请求代码
     * @param perms 请求的权限
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Toast.makeText(this, getString(R.string.somePermisssionsDenied), Toast.LENGTH_LONG).show();
        new AppSettingsDialog.Builder(this).build().show();
    }

}